package bdqn.t09.controller;

import bdqn.t09.pojo.AsKeywords;
import bdqn.t09.pojo.AsSystemconfig;
import bdqn.t09.pojo.AsUser;
import bdqn.t09.service.AsKeywordsService;
import bdqn.t09.service.AsSystemconfigService;
import bdqn.t09.util.Pageinfo;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class KeyWordManagementController {

    @Resource
    private AsKeywordsService asKeywordsService;

    @Resource
    private AsSystemconfigService asSystemconfigService;

    @RequestMapping("/keyWordMan")
    public String gokeyWordManagement(){
        return  "keyWordManagement";
    }

    @ResponseBody
    @RequestMapping("keyWordManShow")
    public Object find(@RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "10") Integer rows,String keywords){
        Pageinfo pageinfo = asKeywordsService.selectKeyWordFen(keywords, page, rows);
        Map map =new HashMap();
        map.put("rows",pageinfo.getList());
        map.put("total",pageinfo.getCount());
        return map;
    }

    @RequestMapping(value = "openAppId", method = RequestMethod.POST)
    @ResponseBody
    public Object openAppId(Long id) {
        AsKeywords asKeywords = asKeywordsService.selectByPrimaryKey(id);
        return asKeywords;
    }

    @RequestMapping(value = "waiAppId", method = RequestMethod.POST)
    @ResponseBody
    public Object waiAppId(String configTypeName) {
        AsSystemconfig asSystemconfig = asSystemconfigService.selectByPrimaryName(configTypeName);
        return asSystemconfig;
    }

    @RequestMapping(value = "kaiApp", method = RequestMethod.POST)
    @ResponseBody
    public Object kaiApp(String userCode,String password,int id, HttpSession session) {
        AsUser user = (AsUser) session.getAttribute("loginUser");
        if(user.getUserpassword().equals(password) && user.getUsercode().equals(userCode)){
            int cout = asKeywordsService.updateByApp(id);
            return cout;
        }
        return -1;
    }



    @RequestMapping(value = "xufei", method = RequestMethod.POST)
    @ResponseBody
    public Object xufei(String keywords,String serviceYears,String price) {
        AsKeywords asKeywords = new AsKeywords();
        asKeywords.setKeywords(keywords);
        asKeywords.setServiceyears(Integer.parseInt(serviceYears));
        asKeywords.setPrice(Double.parseDouble(price));
       int cout = asKeywordsService.updateBykeywords(asKeywords);
       return cout;
    }

    @RequestMapping(value = "LoginUserCode", method = RequestMethod.POST)
    @ResponseBody
    public Object LoginUserCode(String userCode, HttpSession session) {
        AsUser user = (AsUser) session.getAttribute("loginUser");
        if (user.getUsercode().equals(userCode)) {
            return "true";
        }
        return "false";
    }

    @RequestMapping(value = "LoginPassword", method = RequestMethod.POST)
    @ResponseBody
    public Object LoginPassword(String password, HttpSession session) {
        AsUser user = (AsUser) session.getAttribute("loginUser");
        if (user.getUserpassword().equals(password)) {
            return "true";
        }
        return "false";
    }
}